﻿using System;
using System.Windows.Forms;
using cashbook_mg;
using cashbook_psql_cs.BLL;
using cashbook_psql_cs.util;
using DocumentFormat.OpenXml.Wordprocessing;
using Color = System.Drawing.Color;

namespace cashbook_psql_cs
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void MenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            
            RelationBillBLL relationBillBLL = new RelationBillBLL();
            DataGridView1.DataSource = relationBillBLL.FillDatasByRelationBill();      
            DataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            //指定整列的显示格式
            DataGridView1.Columns[5].DefaultCellStyle.Format = "yyyy-MM-dd";
            //DataGridView1.Columns["_id"].Visible = false;
            //禁止dataGridView展示多余的列
            //DataGridView1.AutoGenerateColumns = false;
            //手动计算屏幕分辨率居中
            Screen screen = Screen.PrimaryScreen;
            this.Left = (screen.WorkingArea.Width - this.Width) / 2;
            this.Top = (screen.WorkingArea.Height - this.Height) / 2;
            DataGridView1.AllowUserToOrderColumns = true;
            DataGridView1.EnableHeadersVisualStyles = true;
        }

        private void DateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            string yDate = DateTimePicker2.Value.ToString("yyyy");
            string mDate = DateTimePicker1.Value.ToString("MM");
            string ymDate = yDate + "-" + mDate;
            bool found;
            CurrencyManager cm = (CurrencyManager)BindingContext[DataGridView1.DataSource];
            cm.SuspendBinding(); //将绑定挂起
            string cellValue;
            string _cellValue;

            foreach (DataGridViewRow row in DataGridView1.Rows)
            {
                found = false;
                if (row.IsNewRow)
                {
                    continue;// 跳过新行
                }
                for (int i = 0; i < row.Cells.Count; i++)

                {
                    if (row.Cells[i].ColumnIndex == 5) //判断数据库第五列“时间”
                    {
                        cellValue = row.Cells[i].Value.ToString();
                        if (!string.IsNullOrEmpty(cellValue))
                        {
                            _cellValue = DateTime.Parse(cellValue).ToString("yyyy-MM-dd HH:mm:ss");
                            _cellValue = _cellValue.Substring(0, 7);
                            if (_cellValue.ToLower() == ymDate)
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                }



                row.Visible = found;

            }

            cm.ResumeBinding(); //恢复绑定
        
    }

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string yDate = DateTimePicker2.Value.ToString("yyyy");
            string mDate = DateTimePicker1.Value.ToString("MM");
            string ymDate = yDate + "-" + mDate;
            bool found;
            CurrencyManager cm = (CurrencyManager)BindingContext[DataGridView1.DataSource];
            cm.SuspendBinding(); //将绑定挂起
            string cellValue;
    

            foreach (DataGridViewRow row in DataGridView1.Rows)
            {
                found = false;
                if (row.IsNewRow)
                {
                    continue;// 跳过新行
                }
                for (int i = 0; i < row.Cells.Count; i++)

                {
                    if (row.Cells[i].ColumnIndex == 5) //判断数据库第五列“时间”
                    {
                        cellValue = row.Cells[i].Value.ToString();
                        if ( !string.IsNullOrEmpty(cellValue))
                        {
                            cellValue = cellValue.Substring(0, 7);
                            if (cellValue.ToLower() == ymDate)
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                }



                row.Visible = found;

            }

            cm.ResumeBinding(); //恢复绑定

        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            bool found;
            CurrencyManager cm = (CurrencyManager)BindingContext[DataGridView1.DataSource];
            cm.SuspendBinding(); //将绑定挂起
            string cellValue;
            string txtValue = TextBox1.Text.ToString().ToLower();
            if (!string.IsNullOrEmpty(txtValue))
            {
                foreach (DataGridViewRow row in DataGridView1.Rows)
                {
                    found = false;
                    if (row.IsNewRow)
                    {
                        continue;// 跳过新行
                    }
                    for (int i = 0; i < row.Cells.Count; i++)

                    {
                        cellValue = row.Cells[i].Value.ToString();
                        if (cellValue != null)
                        {
                            if (cellValue.ToLower() == txtValue)
                            {
                                found = true;
                                break;
                            }
                        }
                    }


                    row.Visible = found;

                }
            }
            /*else
            {
                for (int i = 0; i < DataGridView1.Rows.Count; i++)
                {
                    DataGridView1.Rows[i].Visible = true;
                }
            }*/
            cm.ResumeBinding(); //恢复绑定
        }

        private void DateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            string yDate = DateTimePicker2.Value.ToString("yyyy");
            string mDate = DateTimePicker1.Value.ToString("MM");
            string ymDate = yDate + "-" + mDate;
            bool found;
            CurrencyManager cm = (CurrencyManager)BindingContext[DataGridView1.DataSource];
            cm.SuspendBinding(); //将绑定挂起
            string cellValue;
            string _cellValue;

            foreach (DataGridViewRow row in DataGridView1.Rows)
            {
                found = false;
                if (row.IsNewRow)
                {
                    continue;// 跳过新行
                }
                for (int i = 0; i < row.Cells.Count; i++)

                {
                    if (row.Cells[i].ColumnIndex == 5) //判断数据库第五列“时间”
                    {
                        cellValue = row.Cells[i].Value.ToString();
                        if (!string.IsNullOrEmpty(cellValue))
                        {
                            _cellValue =  DateTime.Parse(cellValue).ToString("yyyy-MM-dd HH:mm:ss");
                            _cellValue = _cellValue.Substring(0, 7);
                            if (_cellValue.ToLower() == ymDate)
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                }



                row.Visible = found;

            }

            cm.ResumeBinding(); //恢复绑定
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            FrmAdd frmAdd = new FrmAdd(this); 
            frmAdd.ShowDialog();
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (DataGridView1.SelectedRows.Count == 0) {
                MessageBox.Show("请选择一条要修改的项！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            else {
                string id = DataGridView1.CurrentRow.Cells[0].Value.ToString().Trim();
                string szType = DataGridView1.CurrentRow.Cells[1].Value.ToString().Trim(); 
                string amount = DataGridView1.CurrentRow.Cells[2].Value.ToString().Trim();
                string cate = DataGridView1.CurrentRow.Cells[3].Value.ToString().Trim();
                string account = DataGridView1.CurrentRow.Cells[4].Value.ToString().Trim();
                string szTime = DataGridView1.CurrentRow.Cells[5].Value.ToString().Trim();
                string relPerson = DataGridView1.CurrentRow.Cells[6].Value.ToString().Trim(); ;
                string remark = DataGridView1.CurrentRow.Cells[7].Value.ToString().Trim();
                FrmAdd addForm = new FrmAdd(this);
                addForm.PassedId = Convert.ToInt32(id);
                addForm.ComboBox4.Text = szType;
                addForm.TextBox1.Text = amount;
                addForm.TextBox5.Text = cate;
                addForm.ComboBox2.Text = account;
                addForm.DateTimePicker2.Value = Convert.ToDateTime(szTime);
                addForm.ComboBox3.Text = relPerson;
                addForm.TextBox4.Text = remark;
                //Form3.Visible = True;
                addForm.Button1.Visible = false;
                addForm.ShowDialog();
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            
            if (DataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择一条要删除的项！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else {
                try
                {
                    int id = int.Parse(DataGridView1.CurrentRow.Cells[0].Value.ToString());
                    RelationBill rb = new RelationBill
                    {
                        Id = id
                    };
                    RelationBillBLL relationBillBLL = new RelationBillBLL();
                    relationBillBLL.RemoveRelationBill(rb);

                    MessageBox.Show("删除成功！");
                    DataGridView1.DataSource = relationBillBLL.FillDatasByRelationBill();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

            }   

        }

        
        private void 统计表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmStat form4 = new FrmStat();
            form4.ShowDialog();
        }

        private void 从Excel导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmImport frmImport = new FrmImport(this);
            frmImport.ShowDialog();
        }

        private void 导出到ExcelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "xlsx 文件|*.xlsx";
            saveFileDialog.Title = "导出为 xlsx";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                ExportHelper.ExportToExcel(DataGridView1, saveFileDialog.FileName);
            }
        }

        public DataGridView PublicDataGridView
        {
            get { return DataGridView1; }
            set { DataGridView1 = value; }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }

}
